package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.SparseArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.FindToolsAdp;
import com.bocai.eelly.bean.FindToolsListBean;
import com.bocai.eelly.bean.PlaceBean;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.net.ApiManage;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;
import com.bocai.eelly.views.ExpandTabView;
import com.bocai.eelly.views.XListView;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by admin on 2016/5/31.
 */
public class FindToolsListActivity extends BaseActivity implements View.OnClickListener,XListView.IXListViewListener,AdapterView.OnItemClickListener{

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.search)
    RelativeLayout search;
    @Bind(R.id.expandtab_view)
    ExpandTabView expandtabView;
    @Bind(R.id.lv_find_new_products)
    XListView lvFindNewProducts;
    private int page=1;
    private List<FindToolsListBean.DataEntity> dataEntities=new ArrayList<>();
    private FindToolsAdp findToolsAdp;
    private ArrayList<String> groups = new ArrayList<>();
    private SparseArray<LinkedList<String>> children = new SparseArray<>();
    Observer<String> observer;
    private String province="";
    private String city="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_product_list);
        ButterKnife.bind(this);
        observer=new Observer<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String string) {
                province=string.split(",")[0];
                city=string.split(",")[1];
                onRefresh();
            }
        };
        initView();
        initVaule();
        initSearch(FindToolsListActivity.this, "地区", groups, children,observer);
        initEvent();

    }

    @Override
    public void initListener() {
        super.initListener();

    }

    private void initEvent() {
        lvFindNewProducts.setXListViewListener(this);
        lvFindNewProducts.setPullLoadEnable(false);
        lvFindNewProducts.setPullRefreshEnable(true);
        lvFindNewProducts.setOnItemClickListener(this);
        search.setOnClickListener(this);
    }

    private void initView() {
        getTools();
        ToolbarHelper.setup(this, R.string.str_title_find_tools, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        findToolsAdp=new FindToolsAdp(this,dataEntities);
        lvFindNewProducts.setAdapter(findToolsAdp);
    }

    private void getTools() {
        String timeline= Utils.getTimeline();
        String sign=Utils.md5(timeline);
        getTestApi().findTools(timeline,sign,Utils.getUserId(this,"id"),page,Utils.PAGESIZE,"",province,city)
                .compose(this.<FindToolsListBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<FindToolsListBean>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {
                        lvFindNewProducts.stopRefresh();
                        Loading.dismiss();
                        L.d(e.toString());
                        T.showShort(FindToolsListActivity.this,R.string.net_error);
                    }

                    @Override
                    public void onNext(FindToolsListBean findFabricListBean) {
                        lvFindNewProducts.stopRefresh();
                        Loading.dismiss();
                        L.d(findFabricListBean.getMsg());
                        if (Utils.checkResultCode(findFabricListBean.getReturn_code()))
                        {
                            dataEntities.addAll(findFabricListBean.getData());
                            findToolsAdp.notifyDataSetChanged();
                            if (findFabricListBean.getData().size() < Utils.PAGESIZE) {
                                lvFindNewProducts.setPullLoadEnable(false);
                            }else{
                                lvFindNewProducts.setPullLoadEnable(true);
                            }
                        }else
                        {
                            T.showLong(FindToolsListActivity.this,findFabricListBean.getMsg());
                        }
                    }
                });
    }
    /**
     * 初始化数据
     */
    private void initVaule() {
        String timeline= Utils.getTimeline();
        String sign=Utils.md5(timeline);
        ApiManage.getCity(this, timeline, sign, Utils.getUserId(this, "id"), new Action1<PlaceBean>() {
            @Override
            public void call(PlaceBean placeBean) {
//                T.showShort(FindToolsListActivity.this,placeBean.getMsg());
                L.d(placeBean.getMsg());
                for (int i = 0; i <placeBean.getData().size(); i++) {
                    groups.add(placeBean.getData().get(i).getP());
                    LinkedList<String> tItem = new LinkedList<>();
                    for (int j = 0; j < placeBean.getData().get(i).getC().size(); j++) {
                        tItem.add(placeBean.getData().get(i).getC().get(j).getTitle());
                    }
                    children.put(i, tItem);
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.search:
                Intent intent=new Intent(this,SearchToolsActivity.class);
//                intent.putExtra("province",province);
//                intent.putExtra("city",city);

                startActivity(intent);
                break;
        }
    }

    @Override
    public void onRefresh() {
        page=1;
        dataEntities.clear();
        lvFindNewProducts.setPullLoadEnable(false);
        getTools();
    }

    @Override
    public void onLoadMore() {
        page++;
        getTools();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

}
